package com.location.footprint.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.location.footprint.R;
import com.location.footprint.activity.DynamicActivity;
import com.location.footprint.adapter.FootPrintAdapter;
import com.location.footprint.bean.DataProvider;
import com.location.footprint.bean.FootPrintBean;
import com.location.footprint.manager.DividerDecoration;
import com.location.footprint.utils.LogUtil;
import com.location.footprint.utils.Utils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class HotFootPrintFragment extends BaseFragment {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    Unbinder unbinder;
    private String mTitle;
    private FootPrintAdapter hAdapter;
    private boolean isScrolling;

    public static HotFootPrintFragment getInstance(String title) {
        HotFootPrintFragment sf = new HotFootPrintFragment();
        sf.mTitle = title;
        return sf;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_hot_footprint;
    }

    @Override
    protected void initView() {
        LogUtil.e(mTitle);
        recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerview.setHasFixedSize(true);
        DividerDecoration dividerDecoration = new DividerDecoration(getResources().getColor(R.color.divider_line), Utils.dip2px(getContext(), 8));
        recyclerview.addItemDecoration(dividerDecoration);
        recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_DRAGGING || newState == RecyclerView.SCROLL_STATE_SETTLING) {
                    isScrolling = true;
                    Glide.with(getContext()).pauseRequests();
                } else if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (isScrolling == true) {
                        Glide.with(getContext()).resumeRequests();
                    }
                    isScrolling = false;
                }
            }
        });
        List<FootPrintBean> data = DataProvider.getAdList();
        hAdapter = new FootPrintAdapter(data);
        recyclerview.setAdapter(hAdapter);
        hAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                DynamicActivity.lanuch(getContext(),adapter.getItemViewType(position));
//                ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(getActivity(), view, "view_text_image");
//                Intent mIntent = new Intent(getContext(), DynamicActivity.class);
//                getContext().startActivity(mIntent, options.toBundle());
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }
}